package com.zmn.oms.admin.controller.conf.tips;


import cn.hutool.core.bean.BeanUtil;
import com.zmn.base.plat.engine.common.constant.DubboConsts;
import com.zmn.base.plat.engine.common.util.response.DubboResponseUtils;
import com.zmn.base.product.common.consts.BaseProductConsts;
import com.zmn.base.product.common.dto.categ.CategServProductDRO;
import com.zmn.base.product.common.query.categ.ForeignCategServProductQuery;
import com.zmn.base.product.dubbo.interfaces.categ.serv.CategServProductListRemoteService;
import com.zmn.common.dto2.AMISListData;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.tipsconfig.PictureTipsConfigBService;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.dio.conf.tips.PictureTipsConfigDTO;
import com.zmn.oms.model.entity.tipsconfig.PictureTipsConfig;
import com.zmn.oms.model.entity.tipsconfig.PictureTipsConfigQuery;
import com.zmn.oms.services.interfaces.tipsconfig.PictureTipsConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 基础查询Controller
 *
 * @author weilai
 * @since 2022-08-02 14:11:21
 */
@Controller
@RequestMapping("/tips/config/")
@Slf4j
public class PictureTipsConfigListController extends OmsBaseController {
    @Resource
    private PictureTipsConfigBService pictureTipsConfigBService;

    @Resource
    private PictureTipsConfigService pictureTipsConfigService;

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private CategServProductListRemoteService categServProductListRemoteService;

    /**
     * 列表查询
     *
     * @param query
     * @return ModelAndView
     * @author weilai
     * @since 2022-08-02 14:11:21
     */
    @RequestMapping("list")
    @RequiresPermissions("/tips/config/list.action")
    public ModelAndView list(@ModelAttribute PictureTipsConfigQuery query) {
        ModelAndView mav = new ModelAndView("conf/tips/list");
        return mav;
    }

    /**
     * 查询列表 ajax
     *
     * @param query
     * @return
     * @author liudi
     * @since: 2022/2/18 11:24
     */
    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public ResultDTO ajaxList(PictureTipsConfigQuery query) {
        ForeignCategServProductQuery foreignCategServProductQuery = BeanMapper.map(query, ForeignCategServProductQuery.class);
        foreignCategServProductQuery.setShowType(BaseProductConsts.EC_SHOW_TYPE);
        AMISListData<CategServProductDRO> listData = DubboResponseUtils.resultToThrow(()
                -> categServProductListRemoteService.listPageByQuery(foreignCategServProductQuery));
        List<CategServProductDRO> listDataItems = listData.getItems();
        List<PictureTipsConfigDTO> items = BeanUtil.copyToList(listDataItems, PictureTipsConfigDTO.class);
        LayuiTableDO<PictureTipsConfigDTO> data = new LayuiTableDO<>();
        data.setCount(listData.getTotal());
        data.setData(items);
        return ResultDTO.success(data);
    }

    /**
     * @return
     * @author weilai
     * @since 2022-08-02 14:11:21
     */
    @RequestMapping("detail")
    public ModelAndView detail(@RequestParam(value = "servCategId", defaultValue = "1") Integer servCategId,
                               @RequestParam(value = "categOneId", defaultValue = "1") Integer categOneId,
                               @RequestParam(value = "categId", defaultValue = "1") Integer categId) {

        ModelAndView mav = new ModelAndView("conf/tips/detail");
        PictureTipsConfigQuery pictureTipsConfigQuery = new PictureTipsConfigQuery();
        pictureTipsConfigQuery.setServCategId(servCategId);
        pictureTipsConfigQuery.setCategOneId(categOneId);
        pictureTipsConfigQuery.setCategId(categId);
        List<PictureTipsConfig> list = pictureTipsConfigService.listByQuery(pictureTipsConfigQuery);
        mav.addObject("servCategId", servCategId);
        mav.addObject("categOneId", categOneId);
        mav.addObject("categId", categId);
        mav.addObject("list", list);
        return mav;
    }

    @RequestMapping(value = "/uploader")
    @ResponseBody
    public ResultDTO uploaderImage(MultipartFile file) {
        return super.uploaderImage(file, "/oms/tips/photo");
    }

}
